package com.example.im.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.im.domain.dto.CustGetDTO;
import com.example.im.domain.po.CustInfo;
import com.example.im.mapper.CustInfoMapper;
import com.example.im.service.CustInfoService;
import com.example.im.domain.vo.CustGetVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author HUAWEI
* @description 针对表【cust_info(客户信息表)】的数据库操作Service实现
* @createDate 2024-08-21 09:10:06
*/
@Service
public class CustInfoServiceImpl extends ServiceImpl<CustInfoMapper, CustInfo>
    implements CustInfoService {


    @Resource
    private CustInfoMapper custInfoMapper;




    public CustInfo CacheCustInfo(CustGetDTO custGetDTO){ // 先获取到用户信息
        LambdaQueryWrapper<CustInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustInfo::getCustId, custGetDTO.getCustId())
                .or()
                .eq(CustInfo::getMobile, custGetDTO.getMobile());
        return custInfoMapper.selectOne(wrapper);
    }

    @Override
    public CustGetVO getInfo(CustInfo custInfo, CustGetDTO custGetDTO) {
        if (custInfo == null || !validateCustConditions(custInfo, custGetDTO)) {
            return null;
        }
        CustGetVO custGetVO = new CustGetVO();
        // TODO 调用获取到用户营销记录
//        Long count = custRecordsMapper.getCount(custInfo.getCustId());
        Long count = -1L;
        if (ObjectUtil.isNotEmpty(custGetDTO.getSendLimit()) && count >= custGetDTO.getSendLimit()) {
            return null;
        }
        BeanUtil.copyProperties(custInfo,custGetVO);
        custGetVO.setSendNum(count);

        return custGetVO;
    }

    private boolean validateCustConditions(CustInfo custInfo, CustGetDTO custGetDTO) {
        return !(ObjectUtil.isNotEmpty(custGetDTO.getPushRatio()) && custGetDTO.getPushRatio() >= custInfo.getPushRatio() * 100)
                && !(ObjectUtil.isNotEmpty(custGetDTO.getRiskRatio()) && custGetDTO.getRiskRatio() <= custInfo.getRiskRatio() * 100);
    }


    @Override
    public boolean updateById(CustInfo custInfo) {
        if(ObjectUtil.isNotEmpty(custInfo.getCustId())){
            return custInfoMapper.updateById(custInfo) > 0;
        }
        return false;
    }
}




